
//
//  MCChooseForPlatformCell.swift
//  StoreManage
//
//  Created by MC on 2018/2/5.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

protocol MCChooseForPlatformCellDelegate : NSObjectProtocol {
    func MCChooseForPlatformCellToShelve(row:Int,button:UIButton)
}

class MCChooseForPlatformCell: UITableViewCell {

    var row = 0
    weak var delegate : MCChooseForPlatformCellDelegate?
    
    var model : MCChooseForPlatformModel = MCChooseForPlatformModel() {
        didSet {
            iconImageView.sd_setImage(with: URL.init(string: model.goodsPicture), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            nameLabel.text = model.goodsName
            switch model.channel {
            case 2:
                sourceImageView.image = #imageLiteral(resourceName: "GoodsManage_store")
            default:
                sourceImageView.image = nil
                break
            }
            
            if model.isOnShelves == 0 {
                statusbutton.isSelected = false
                statusbutton.isUserInteractionEnabled = true
                selectedButton.isHidden = false
                selectedButton.isSelected = model.isSelected
            } else {
                statusbutton.isSelected = true
                statusbutton.isUserInteractionEnabled = false
                selectedButton.isEnabled = false
                selectedButton.setImage(#imageLiteral(resourceName: "GoodsManage_selected").MCImageWithTintColor(color: UIColor.MCGray_middle), for: UIControlState.normal)
            }
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.addSubview(selectedButton)
        selectedButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(1)
        }
        
        self.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(self)
            make.left.equalTo(selectedButton.snp.right)
            make.size.equalTo(CGSize.init(width: 80, height: 80))
        }
        
        iconImageView.addSubview(sourceImageView)
        sourceImageView.snp.remakeConstraints { (make) ->Void in
            make.left.top.equalTo(iconImageView)
            make.size.equalTo(CGSize.init(width: 30, height: 30))
        }
        
        self.addSubview(arrowImageView)
        arrowImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-10)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize.init(width: 8, height: 14))
        }
        
        self.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.top.equalTo(iconImageView)
            make.right.equalTo(arrowImageView.snp.left).offset(-5)
            make.bottom.lessThanOrEqualTo(lineView.snp.top).offset(-40)
        }
        
        self.addSubview(statusbutton)
        statusbutton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(nameLabel)
            make.bottom.equalTo(lineView.snp.top).offset(-10)
            make.size.equalTo(CGSize.init(width: 45, height: 20))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func selectedButtonClicked() {
        selectedButton.isSelected = !selectedButton.isSelected
        model.isSelected = selectedButton.isSelected
    }
    
    @objc func statusbuttonClicked() {

        delegate?.MCChooseForPlatformCellToShelve(row: row, button: statusbutton)
    }
    
    lazy var selectedButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: UIControlState.normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: UIControlState.selected)
        button.addTarget(self, action: #selector(selectedButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        return imageView
    }()
    
    lazy var sourceImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()

    
    
    lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_arrow")
        return imageView
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.font = MCFont(14)
        return label
    }()
    
    lazy var statusbutton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(10)
        
        button.setBackgroundImage(UIImage.init(color: UIColor.MCMain), for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitle("上架", for: UIControlState.normal)
        
        button.setBackgroundImage(UIImage.init(color: UIColor.MCGray_middle), for: UIControlState.selected)
        button.setTitle("已上架", for: UIControlState.selected)
        button.setTitleColor(UIColor.white, for: UIControlState.selected)

        
        button.addTarget(self, action: #selector(statusbuttonClicked), for: UIControlEvents.touchUpInside)
        
        return button
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
}
